package com.product.annotation.aspect;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.EquityStatus;
import com.product.dao.ProductEquityDao;
import com.product.entity.ProductEquity;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: Member_Perks_Store
 * @description: 库存扣减监听器,实现下架
 * @author: 无恙
 * @create: 2024-06-18 15:31
 **/


@Aspect
@Component
@Scope
public class InventoryAspect {

    @Autowired
    ProductEquityDao productEquityDao;

    @Around(value = "execution(* com.product.service.IEquityDetailed.updateInventory(..)) && args(Id, quantity)", argNames = "joinPoint,Id,quantity")
    @Transactional
    public Object aroundInventoryUpdate(ProceedingJoinPoint joinPoint, Long Id, int quantity) throws Throwable {
        // 执行目标方法
        Object result = joinPoint.proceed();

        // 在同一个事务中处理下线逻辑
        if (result instanceof Integer && (int) result == 0) {
            ProductEquity entityDetailed = productEquityDao.selectOne(new QueryWrapper<ProductEquity>()
                    .eq("entity_detailed_id", Id)
            );
            entityDetailed.setProductState(EquityStatus.OFFLINE.getCode());
            productEquityDao.updateById(entityDetailed);
        }

        return result;
    }




}
